package com.bigcreate.controller;

import com.alibaba.fastjson.JSON;
import com.bigcreate.entity.Books;
import com.bigcreate.entity.User;
import com.bigcreate.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    @CrossOrigin
    @RequestMapping(value = "addBook")
    public String addBook(@RequestBody Books book){

        //System.out.println(book);
        bookService.addBook(book);
        return "ok";
    }

    @CrossOrigin
    @RequestMapping("/deleteBook")
    public String deleteBook(int id){
        bookService.deleteBookById(id);
        return "ok";
    }

    @CrossOrigin
    @RequestMapping(value = "/updateBook")
    public String updateBook(@RequestBody Books book){
        bookService.updateBook(book);
        return "ok";
    }

    // 如果采用url路径传参，通过@PathVariable来接收
    // ajax法
    // 会自动分割，前提是和前台参数名一致，否则使用@RequestParam
    @CrossOrigin
    @RequestMapping("/updateNum")
    public String updateBookNum(int add, int number, int id){
        bookService.updateBookNum(id,number,add);
        return "ok";
    }

    @CrossOrigin
    @RequestMapping("/searchId")
    public Books searchId(int id){
        Books books = bookService.queryBookById(id);
        return books;
    }

    @CrossOrigin
    @RequestMapping("/searchAll")
    public List<Books> searchAll(){
        List<Books> books = bookService.queryAll();
        return books;
    }

    @CrossOrigin
    @RequestMapping("/searchName")
    public String searchName(String bookName){
        List<Books> books = bookService.queryBookByName(bookName);
        return JSON.toJSONString(books);
    }

    @CrossOrigin
    @RequestMapping("/login")
    public String judge(String name, String password){
        User user = bookService.getUser(name);
        Boolean flag = false;
        if(user.getPassword().equals(password)) flag = true;
        return JSON.toJSONString(flag);
    }









}
